﻿using System.Security.Cryptography;
using System.Text;
using System.Windows;
using Resn.WhuPub.BLL;
using Resn.WhuPub.DAL;
using Resn.WhuPub.DML;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现员工的属性和方法
        /// </summary>
        public class Employee :Entity<EmployeeModel>
        {
                #region 构造函数

                /// <summary>
                /// 创建员工实例
                /// </summary>
                /// <param name="model">员工模型</param>
                public Employee(EmployeeModel model)
                        : base(model)
                {
                }
                #endregion

                #region 属性

                #region 基本属性

                /// <summary>
                /// 获取或设置员工姓名
                /// </summary>
                /// <value>
                /// 员工姓名
                /// </value>
                public string Name
                {
                        get
                        {
                                return Model.Name;
                        }
                        set
                        {
                                Model.Name = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工密码
                /// </summary>
                /// <value>
                /// 员工密码
                /// </value>
                public string Password
                {
                        get
                        {
                                return Model.Password;
                        }
                        set
                        {
                                Model.Password = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工层次
                /// </summary>
                /// <value>
                /// 员工层次
                /// </value>
                public EmployeeLevel Level
                {
                        get
                        {
                                return (EmployeeLevel)Enum.Parse(typeof(EmployeeLevel), Model.Level);
                        }
                        set
                        {
                                Model.Level = value.ToString();
                        }
                }

                /// <summary>
                /// 获取或设置员工状态
                /// </summary>
                /// <value>
                /// 员工状态
                /// </value>
                public EmployeeState State
                {
                        get
                        {
                                return (EmployeeState)Enum.Parse(typeof(EmployeeState), Model.State);
                        }
                        set
                        {
                                Model.State = value.ToString();
                        }
                }

                /// <summary>
                /// 获取或设置员工联系电话和手机
                /// </summary>
                /// <value>
                /// 员工联系电话和手机，多个电话用分号隔开
                /// </value>
                public string Duty
                {
                        get
                        {
                                return Model.Duty;
                        }
                        set
                        {
                                Model.Duty = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工联系电话和手机
                /// </summary>
                /// <value>
                /// 员工联系电话和手机，多个电话用分号隔开
                /// </value>
                public string Telephone
                {
                        get
                        {
                                return Model.Telephone;
                        }
                        set
                        {
                                Model.Telephone = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工QQ
                /// </summary>
                /// <value>
                /// 员工QQ，多个QQ用分号隔开
                /// </value>
                public string QQ
                {
                        get
                        {
                                return Model.QQ;
                        }
                        set
                        {
                                Model.QQ = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工电子邮箱
                /// </summary>
                /// <value>
                /// 员工电子邮箱，多个邮箱用分号隔开
                /// </value>
                public string Email
                {
                        get
                        {
                                return Model.Email;
                        }
                        set
                        {
                                Model.Email = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工权限项集合字符串
                /// </summary>
                /// <value>
                /// 员工权限项集合字符串,多项权限项用分号隔开
                /// </value>
                public string Access
                {
                        get
                        {
                                return Model.Access;
                        }
                        set
                        {
                                Model.Access = value;
                        }
                }

                /// <summary>
                /// 获取或设置员工备注
                /// </summary>
                /// <value>
                /// 员工备注
                /// </value>
                public string Memo
                {
                        get
                        {
                                return Model.Memo;
                        }
                        set
                        {
                                Model.Memo = value;
                        }
                }

                #endregion 基本属性

                public string FullName
                {
                        get { return Name + " " + Duty; }
                }

                /// <summary>
                /// 获取权限项集合
                /// </summary>
                /// <value>
                /// 权限项集合
                /// </value>
                public List<string> Provices
                {
                        get
                        {
                                return Model.Access.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        }
                }

                public List<Corporate> Corporates
                {
                        get
                        {
                                switch (Level)
                                {
                                        case EmployeeLevel.普通用户:
                                                return GlobalManager.Instance.CorporateEntityManager.Entitys.Where(
                                                                i => ContainsAccess(i.MainBranch.Province))
                                                          .ToList();
                                                break;

                                        default:
                                                return GlobalManager.Instance.CorporateEntityManager.Entitys;
                                }
                        }
                }

                public List<Branch> Branchs
                {
                        get 
                        { 
                                var corporates = Corporates;

                                var result = new List<Branch>();

                                corporates.ForEach(i=>result.AddRange(i.Branchs));

                                return result;
                        }
                }

                public List<Department> Departments
                {
                        get
                        {
                                var branchs = Branchs;

                                var result = new List<Department>();

                                branchs.ForEach(i => result.AddRange(i.Departments));

                                return result;
                        }
                }

                public List<Contact> Contacts
                {
                        get
                        {
                                var departments = Departments;

                                var result = new List<Contact>();

                                departments.ForEach(i => result.AddRange(i.Contacts));

                                return result;
                        }
                }

                public List<FeedbackFlow> FeedbackFlows
                {
                        get
                        {
                                var contacts = Contacts;

                                var result = new List<FeedbackFlow>();

                                contacts.ForEach(i => result.AddRange(i.FeedbackFlows));

                                return result;
                        }
                }

                public List<FeedbackFlow> UnCompleteFeedbackFlows
                {
                        get { return FeedbackFlows.Where(i => i.State != FeedbackFlowState.已完结).ToList(); }
                }

                public List<BookFlow> BookFlows
                {
                        get
                        {
                                var contacts = Contacts;

                                var result = new List<BookFlow>();

                                contacts.ForEach(i => result.AddRange(i.BookFlows));

                                return result;
                        }
                }

                public List<BookFlow> UnCompleteBookFlows
                {
                        get { return BookFlows.Where(i => i.State != BookFlowState.已完结).ToList(); }
                }

                public List<Dealer> Dealers
                {
                        get
                        {
                                switch (Level)
                                {
                                        case EmployeeLevel.普通用户:
                                                return GlobalManager.Instance.DealerEntityManager.Entitys.Where(
                                                                i => ContainsAccess(i.Province))
                                                          .ToList();
                                                break;

                                        default:
                                                return GlobalManager.Instance.DealerEntityManager.Entitys;
                                }
                        }
                }

                public List<InvoiceFlow> InvoiceFlows
                {
                        get
                        {
                                var dealers = Dealers;

                                var result = new List<InvoiceFlow>();

                                dealers.ForEach(i => result.AddRange(i.InvoiceFlows));

                                return result;
                        }
                }

                public List<InvoiceFlow> UnCompleteInvoiceFlows
                {
                        get { return InvoiceFlows.Where(i => i.State != InvoiceState.已完结).ToList(); }
                }

                public List<TravelFlow> TravelFlows
                {
                        get
                        {
                                switch (Level)
                                {
                                        case EmployeeLevel.普通用户:
                                                return GlobalManager.Instance.TravelFlowEntityManager.Entitys.Where(
                                                                i => i.TravelEmployeeId == Id)
                                                          .ToList();

                                        default:
                                                return GlobalManager.Instance.TravelFlowEntityManager.Entitys;
                                }
                        }
                }

                public List<TravelFlow> UnCompleteTravelFlows
                {
                        get { return TravelFlows.Where(i => i.State != TravelFlowState.已经完结).ToList(); }
                }

                public Visibility VisibleForAdmin
                {
                        get
                        {
                                if (Level == EmployeeLevel.管理员)
                                {
                                        return Visibility.Visible;
                                }
                                return  Visibility.Collapsed;
                        }
                }

                public Visibility VisibleForEmployee
                {
                        get
                        {
                                if (Level == EmployeeLevel.普通用户)
                                {
                                        return Visibility.Visible;
                                }
                                return Visibility.Collapsed;
                        }
                }

                public bool TrueForAdmin
                {
                        get
                        {
                                if (Level == EmployeeLevel.管理员)
                                {
                                        return true;
                                }
                                return false;
                        }
                }

                public bool TrueForEmployee
                {
                        get
                        {
                                if (Level == EmployeeLevel.管理员)
                                {
                                        return false;
                                }
                                return true;
                        }
                }

                

                #endregion 属性

                #region 方法

                /// <summary>
                /// 添加员工到本员工分组
                /// </summary>
                /// <param name="name">账号</param>
                /// <param name="password">密码</param>
                /// <param name="level">层次</param>
                /// <returns></returns>
                public static Employee CreateEmployee(string name, string password, EmployeeLevel level = EmployeeLevel.普通用户, string duty ="", EmployeeState state = EmployeeState.在职,
                        string telephone="", string qq="", string email="", string access="", string memo = "")
                {
                        var model = new EmployeeModel
                        {
                                Id = Guid.NewGuid().ToString(),
                                Name = name,
                                Password = password,
                                Duty = duty,
                                Level = level.ToString(),
                                State = state.ToString(),
                                Telephone = telephone,
                                QQ = qq,
                                Email = email,
                                Access = access,
                                Memo = memo
                        };

                        return new Employee(model);
                }

                /// <summary>
                /// 判断权限项集合字符串是否包含指定的权限项
                /// </summary>
                /// <param name="province">权限项</param>
                /// <returns>
                /// True：包含
                /// False：不包含
                /// </returns>
                public bool ContainsAccess(string province)
                {
                        return Provices.Contains(province);
                }

                /// <summary>
                /// 创建差旅流程
                /// </summary>
                /// <param name="content">计划内容</param>
                /// <param name="lendAmount">借款金额</param>
                /// <param name="startTime">开始时间</param>
                /// <param name="endTime">结束时间</param>
                /// <param name="memo">备注</param>
                /// <returns>
                /// 创建的差旅对象
                /// </returns>
                public TravelFlow CreateTravelFlow(string content, double lendAmount, DateTime startTime, DateTime endTime, string memo, string approveEmployeeId)
                {
                        var model = new TravelFlowModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        TravelEmployeeId = Id,
                                        Content = content,
                                        State = TravelFlowState.等待审批.ToString(),
                                        Modify = string.Empty,
                                        StartTime = startTime,
                                        EndTime = endTime,
                                        Memo = memo,
                                        ApproveEmployeeId = approveEmployeeId
                                };
                        return  new TravelFlow(model);
                }

                public static String ComputerPassword(string source)
                {
                        return Encoding.Default.GetString(MD5.Create().ComputeHash(Encoding.Default.GetBytes(source)));
                }

                #endregion 方法
        }
}